package com.guigu.pd.dispatch.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.guigu.pd.dispatch.common.CustomIdGenerator;
import com.guigu.pd.dispatch.entity.ScheduleJobLogEntity;
import com.guigu.pd.dispatch.service.ScheduleJobLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.guigu.pd.dispatch.entity.ScheduleJobEntity;
import com.guigu.pd.dispatch.service.ScheduleJobService;
import com.guigu.common.utils.PageUtils;
import com.guigu.common.utils.R;



/**
 * 定时任务
 *
 * @author deyran
 * @email deyran@gmail.com
 * @date 2022-07-19 19:22:09
 */
@RestController
@RequestMapping("dispatch/schedulejob")
public class ScheduleJobController {
    @Autowired
    private ScheduleJobService scheduleJobService;

    @Autowired
    private ScheduleJobLogService scheduleJobLogService;

    @Autowired
    private CustomIdGenerator customIdGenerator;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = scheduleJobService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 根据机构编号查询智能调度表数据
     * 查询该机构智能调度日志数据
     */
    @GetMapping("/info/{coid}")
    public ScheduleJobEntity  info(@PathVariable("coid") String coid){
        LambdaQueryWrapper<ScheduleJobEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ScheduleJobEntity::getBusinessId,coid);
        ScheduleJobEntity one = scheduleJobService.getOne(lambdaQueryWrapper);

        if(one==null){
            return new ScheduleJobEntity();
        }

        LambdaQueryWrapper<ScheduleJobLogEntity> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper1.eq(ScheduleJobLogEntity::getJobId,one.getId());
        List<ScheduleJobLogEntity> list = scheduleJobLogService.list(lambdaQueryWrapper1);

        one.setScheduleJobLogEntities(list);

        return one;


    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestParam Map<String, Object> params){
        ScheduleJobEntity scheduleJob = JSON.parseObject(JSON.toJSONString(params), ScheduleJobEntity.class);
        scheduleJob.setId(customIdGenerator.nextId(scheduleJob).toString());

        scheduleJobService.save(scheduleJob);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestParam Map<String, Object> params){
        ScheduleJobEntity scheduleJob = JSON.parseObject(JSON.toJSONString(params), ScheduleJobEntity.class);
        LambdaUpdateWrapper<ScheduleJobEntity> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(ScheduleJobEntity::getBusinessId,scheduleJob.getBusinessId())
                .set(ScheduleJobEntity::getCronExpression,scheduleJob.getCronExpression())
                .set(ScheduleJobEntity::getStatus,scheduleJob.getStatus())
                .set(ScheduleJobEntity::getRemark,scheduleJob.getRemark());
		scheduleJobService.update(lambdaUpdateWrapper);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody String[] ids){
		scheduleJobService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }



}
